import Util from 'util/mm.jsx';
const _mm = new Util();


class Product{
	
	getProductList(listParam){
		let url 	= '',
			data	= {};
		data.pageNum = listParam.pageNum
		if (listParam.listType === 'list') {
			url = '/manage/product/list.do';
		} else {
			//搜索的方式获取列表
			url = '/manage/product/search.do';
			data[listParam.searchType] = listParam.searchKeyWord;
		}
		return  _mm.request({url,data});
	}
	
	setProductStatus(productId,status){
		return  _mm.request({
			url:'/manage/product/set_sale_status.do',
			data:{productId,status}
		});
	}
	
	getCategoryList(categoryId){
		return  _mm.request({
			url:'/manage/category/get_category.do',
			data:{categoryId:categoryId || 0}
		});
	}
	
	updateCategoryName(categoryId,categoryName){
		return  _mm.request({
			url:'/manage/category/set_category_name.do',
			data:{categoryId,categoryName}
		});
	}
	
	addCategory(categoryInfo){
		return  _mm.request({
			url:'/manage/category/add_category.do',
			data:categoryInfo
		});
	}
	
	checkCategory(categoryInfo){
		
		if (typeof categoryInfo.parentId !== 'number') {
			return {
				status:false,
				msg:'品类所属ID无效'
			}
		}
		
		if (typeof categoryInfo.categoryName !== 'string' || categoryInfo.categoryName.length === 0) {
			return {
				status:false,
				msg:'品类名称不能为空'
			}
		}
		
		return {
			status:true,
			msg:'验证通过'
		}
	}
	
	checkProduct(productInfo){
		if (typeof productInfo.name !== 'string' || productInfo.name.length === 0) {
			return {
				status:false,
				msg:'商品名称不能为空'
			}
		}
		
		if (typeof productInfo.subtitle !== 'string' || productInfo.subtitle.length === 0) {
			return {
				status:false,
				msg:'商品描述不能为空'
			}
		}
		
		if (typeof productInfo.categoryId !== 'number' || productInfo.categoryId === 0) {
			return {
				status:false,
				msg:'商品品类无效'
			}
		}
		if (typeof productInfo.price !== 'number' || productInfo.price < 0) {
			return {
				status:false,
				msg:'请输入正确的商品价格'
			}
		}
		if (typeof productInfo.stock !== 'number' || productInfo.stock < 0) {
			return {
				status:false,
				msg:'请输入正确的商品库存'
			}
		}
		return {
			status:true,
			msg:'验证通过'
		}
	}
	
	saveProduct(productInfo){
		return _mm.request({
			url     : '/manage/product/save.do',
			data    : productInfo
		});
	}
	
	getProduct(productId){
		return _mm.request({
			url     : '/manage/product/detail.do',
			data    : {productId}
		});
	}
}

export default Product;